//
//  HLSharePosterCell.m
//  YueYue
//
//  Created by hulu001 on 2023/9/21.
//

#import "HLSharePosterCell.h"
#import "HLQRCodeImage.h"

@interface HLSharePosterCell ()

@property (nonatomic, strong) UIView *containerView;
@property (nonatomic, strong) UIImageView *imageView;
@property (nonatomic, strong) UIView *bottomView;
@property (nonatomic, strong) UILabel *invitationLabel;
@property (nonatomic, strong) QMUIButton *qrTipView;
@property (nonatomic, strong) UIImageView *qrView;

@end

@implementation HLSharePosterCell


-(instancetype)initWithFrame:(CGRect)frame{
    
    if ([super initWithFrame:frame]) {
        self.backgroundColor= UIColorWhite;
        [self setupView];
    }
    return self;
    
}

- (void)setupView
{
    self.containerView = [[UIView alloc] init];
    [self.contentView addSubview:_containerView];
    [_containerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.mas_equalTo(6);
        make.right.bottom.mas_equalTo(-6);
    }];
    
    self.imageView = [[UIImageView alloc] init];
    _imageView.image = UIImageMake(@"share_poster1");
    _imageView.contentMode = UIViewContentModeScaleToFill;
    [_containerView addSubview:_imageView];
    [_imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.mas_equalTo(0);
        make.right.bottom.mas_equalTo(0);
    }];
    
    
    self.bottomView = [[UIView alloc] init];
    _bottomView.backgroundColor = UIColorWhite;
    _bottomView.layer.masksToBounds = YES;
    _bottomView.layer.cornerRadius = 16;
    [_containerView addSubview:_bottomView];
    [_bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(12);
        make.right.mas_equalTo(-12);
        make.bottom.mas_equalTo(-22);
    }];
    
    self.qrView = [[UIImageView alloc] init];
    [_bottomView addSubview:_qrView];
    [_qrView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(48);
        make.top.mas_equalTo(10);
        make.right.bottom.mas_equalTo(-10);
    }];
    
    self.invitationLabel = [[UILabel alloc] qmui_initWithFont:UIFontMediumMake(11) textColor:HLTextColor1];
    _invitationLabel.text = [NSString stringWithFormat:@"我的邀请码：%@",HLLoginModel.dataWithKeychain.inviteNo];
    [_bottomView addSubview:_invitationLabel];
    [_invitationLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.mas_equalTo(10);
        make.right.mas_equalTo(-56);
    }];
    
    self.qrTipView = [[QMUIButton alloc] init];
    [_qrTipView setTitle:@"长按识别二维码" forState:UIControlStateNormal];
    [_qrTipView setImage:UIImageMake(@"share_right") forState:UIControlStateNormal];
    [_qrTipView setTitleColor:UIColorWhite forState:UIControlStateNormal];
    [_qrTipView setBackgroundColor:UIColorBlack];
    _qrTipView.imagePosition = QMUIButtonImagePositionRight;
    _qrTipView.spacingBetweenImageAndTitle = 8;
    _qrTipView.contentEdgeInsets = UIEdgeInsetsMake(2, 6, 2, 6);
    _qrTipView.titleLabel.font = UIFontMediumMake(11);
    _qrTipView.layer.masksToBounds = YES;
    _qrTipView.layer.cornerRadius = 10;
    [_bottomView addSubview:_qrTipView];
    [_qrTipView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_invitationLabel);
        make.bottom.mas_equalTo(-10);
    }];
    
}


-(void)setModel:(HLShareLinkModel *)model
{
    _model = model;
    [_imageView sd_setImageWithURL:[NSURL URLWithString:model.posterImgUrl] placeholderImage:UIImageMake(@"share_poster1")];
    NSString *linkString = [model.shareLink stringByAppendingFormat:@"?channelCode=SHARE&lang=%@&inviteCode=%@",[HLHelper systemLang],HLLoginModel.dataWithKeychain.inviteNo];
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        UIImage *image = [HLQRCodeImage QRImageWithString:linkString size:100 logo:nil];
        dispatch_sync(dispatch_get_main_queue(), ^{
            self.qrView.image = image;
        });
    });
    
}

- (UIImage *)capture
{
    UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, [UIScreen mainScreen].scale);

    [self.layer renderInContext:UIGraphicsGetCurrentContext()];

    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();
    
    return image;
}

+ (NSString *)identifyCell
{
    return NSStringFromClass([self class]);
}

@end
